package com.example.ch03xyztwocom.vo;

import com.example.ch03xyztwocom.enums.ResultCode;
import lombok.Data;

import java.io.Serializable;

@Data
public class ResultJson<T> implements Serializable{

    private static final long serialVersionUID = 783015033603078674L;

    private int code;

    private String message;

    private T data;


    public static ResultJson success() {
        return success("");
    }

    public static ResultJson success(Object o) {
        return new ResultJson(ResultCode.SUCCESS, o);
    }
    public static ResultJson success(String message) {
        return new ResultJson(ResultCode.SUCCESS.getCode(),message );
    }
    public static ResultJson success(Object o,String message) {
        return new ResultJson(message,ResultCode.SUCCESS, o);
    }
    public static ResultJson failure(){
        return failure("");
    }
    public static ResultJson failure(String message){
        return new ResultJson(ResultCode.SERVER_ERROR.getCode(),message);
    }
    public static ResultJson failure(ResultCode code) {
        return new ResultJson(code);
    }
    public static ResultJson failure(ResultCode code,String message) {
        return new ResultJson(code.getCode(),message);
    }

    public static ResultJson failure(ResultCode code, Object o ) {
        return new ResultJson(code, o);
    }

    public ResultJson(String message, int code, T data) {
        this.code=code;
        this.data = data;
        this.message=message;
    }

    public ResultJson(String message, ResultCode resultCode, T data) {
        this.code=resultCode.getCode();
        this.data = data;
        this.message=message;
    }
    public ResultJson(ResultCode resultCode, T data){
        this.code=resultCode.getCode();
        this.data = data;
        this.message=resultCode.getMsg();
    }

    public ResultJson(int code, String message) {
        this.code = code;
        this.message=message;
    }
    public ResultJson(ResultCode code) {
        this.code = code.getCode();
        this.message=code.getMsg();
    }
    @Override
    public String toString() {
        return "{" +
                "\"code\":" + code +
                ", \"message\":\"" + message + '\"' +
                ", \"data\":\"" + data + '\"'+
                '}';
    }
}
